<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Title</title>
    <style type="text/css">
      html,
      body {
        margin: 0;
        height: 100%;
      }

      canvas {
        display: block;
      }
      .myexplain {
        position: absolute;
        top: 10px;
        left: 100px;
        z-index: 99;
        background: #fff;
        margin: 0;
        padding: 0;
      }
    </style>
  </head>
  <body onload="draw();">
    <pre class="myexplain">
position(位置) 决定该对象相对于父对象的位置
rotation(旋转) 通过此属性可以设置对象任意轴的旋转幅度
scale(比例) 通过这个属性可设置沿着任意轴缩放对象
translateX 沿着x轴平移对象
translateY 沿着y轴平移对象
translateZ 沿着z轴平移对象
visible 是否渲染对象
    </pre>
  </body>
  <script src="https://cdn.bootcdn.net/ajax/libs/three.js/r128/three.js"></script>
  <script src="./js/controls/OrbitControls.js"></script>
  <script src="./js/libs/stats.min.js"></script>
  <script src="./js/libs/dat.gui.min.js"></script>
  <script>
    var renderer;
    function initRender() {
      renderer = new THREE.WebGLRenderer({ antialias: true });
      renderer.setSize(window.innerWidth, window.innerHeight);
      //告诉渲染器需要阴影效果
      renderer.shadowMap.enabled = true;
      renderer.shadowMap.type = THREE.PCFSoftShadowMap; // 默认的是，没有设置的这个清晰 THREE.PCFShadowMap
      document.body.appendChild(renderer.domElement);
    }

    var camera;
    function initCamera() {
      camera = new THREE.PerspectiveCamera(
        45,
        window.innerWidth / window.innerHeight,
        0.1,
        1000
      );
      camera.position.set(0, 40, 100);
      camera.lookAt(new THREE.Vector3(0, 0, 0));
    }

    var scene;
    function initScene() {
      scene = new THREE.Scene();
    }

    //初始化dat.GUI简化试验流程
    var settings;
    function initGui() {
      //声明一个保存需求修改的相关数据的对象
      settings = {
        positionX: 0,
        positionY: 5,
        positionZ: 0,
        rotationX: 0,
        rotationY: 0,
        rotationZ: 0,
        scaleX: 1,
        scaleY: 1,
        scaleZ: 1,
        translateX: 0,
        translateY: 0,
        translateZ: 0,
        translate: function () {
          //boxg.translate(settings.translateX,settings.translateY,settings.translateZ);
          boxg.translateX(settings.translateX);
          boxg.translateY(settings.translateY);
          boxg.translateZ(settings.translateZ);

          settings.positionX = boxg.position.x;
          settings.positionY = boxg.position.y;
          settings.positionZ = boxg.position.z;
        },
        visible: true,
      };

      //初始化gui
      var gui = new dat.GUI();
      // 位置 缩放 旋转 平移 是否渲染
      var position = gui.addFolder("position");
      position.add(settings, "positionX", -30, 30).listen();
      position.add(settings, "positionY", -30, 30).listen();
      position.add(settings, "positionZ", -30, 30).listen();
      var scale = gui.addFolder("scale");
      scale.add(settings, "scaleX", 0.01, 5);
      scale.add(settings, "scaleY", 0.01, 5);
      scale.add(settings, "scaleZ", 0.01, 5);
      var rotation = gui.addFolder("rotation");
      rotation.add(settings, "rotationX", -2 * Math.PI, 2 * Math.PI);
      rotation.add(settings, "rotationY", -2 * Math.PI, 2 * Math.PI);
      rotation.add(settings, "rotationZ", -2 * Math.PI, 2 * Math.PI);
      var translate = gui.addFolder("translate");
      translate.add(settings, "translateX", -5, 5);
      translate.add(settings, "translateY", -5, 5);
      translate.add(settings, "translateZ", -5, 5);
      translate.add(settings, "translate");
      gui.add(settings, "visible");
    }

    var light;
    function initLight() {
      scene.add(new THREE.AmbientLight(0x444444));

      light = new THREE.PointLight(0xffffff);
      light.position.set(15, 30, 10);

      //告诉平行光需要开启阴影投射
      light.castShadow = true;

      scene.add(light);
    }

    var boxg;
    function initModel() {
      //辅助工具
      var helper = new THREE.AxisHelper(10);
      scene.add(helper);

      //立方体
      var BoxGeometry = new THREE.BoxGeometry(10, 10, 10);
      var boxgMaterial = new THREE.MeshLambertMaterial({ color: 0x00ffff });

      boxg = new THREE.Mesh(BoxGeometry, boxgMaterial);

      //告诉立方体需要投射阴影
      boxg.castShadow = true;

      scene.add(boxg);

      //底部平面
      var planeGeometry = new THREE.PlaneGeometry(100, 100);
      var planeMaterial = new THREE.MeshStandardMaterial({ color: 0xaaaaaa });

      var plane = new THREE.Mesh(planeGeometry, planeMaterial);
      plane.rotation.x = -0.5 * Math.PI;
      plane.position.y = -0;

      //告诉底部平面需要接收阴影
      plane.receiveShadow = true;

      scene.add(plane);
    }

    //初始化性能插件
    var stats;
    function initStats() {
      stats = new Stats();
      document.body.appendChild(stats.dom);
    }

    //用户交互插件 鼠标左键按住旋转，右键按住平移，滚轮缩放
    var controls;
    function initControls() {
      controls = new THREE.OrbitControls(camera, renderer.domElement);

      // 如果使用animate方法时，将此函数删除
      //controls.addEventListener( 'change', render );
      // 使动画循环使用时阻尼或自转 意思是否有惯性
      controls.enableDamping = true;
      //动态阻尼系数 就是鼠标拖拽旋转灵敏度
      //controls.dampingFactor = 0.25;
      //是否可以缩放
      controls.enableZoom = true;
      //是否自动旋转
      controls.autoRotate = false;
      //设置相机距离原点的最远距离
      controls.minDistance = 100;
      //设置相机距离原点的最远距离
      controls.maxDistance = 200;
      //是否开启右键拖拽
      controls.enablePan = true;
    }

    function render() {
      renderer.render(scene, camera);
    }

    //窗口变动触发的函数
    function onWindowResize() {
      camera.aspect = window.innerWidth / window.innerHeight;
      camera.updateProjectionMatrix();
      renderer.setSize(window.innerWidth, window.innerHeight);
    }

    function animate() {
      //更新控制器
      render();

      //更新性能插件
      stats.update();

      //更新相关位置
      boxg.position.set(
        settings.positionX,
        settings.positionY,
        settings.positionZ
      );
      boxg.scale.set(settings.scaleX, settings.scaleY, settings.scaleZ);
      boxg.rotation.set(
        settings.rotationX,
        settings.rotationY,
        settings.rotationZ
      );
      boxg.visible = settings.visible;

      controls.update();

      requestAnimationFrame(animate);
    }

    function draw() {
      initGui();
      initRender();
      initScene();
      initCamera();
      initLight();
      initModel();
      initControls();
      initStats();

      animate();
      window.onresize = onWindowResize;
    }
  </script>
</html>
